package com.myc.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myc.train.common.exception.BusinessException;
import com.myc.train.common.exception.BusinessExceptionEnum;
import com.myc.train.common.resp.PageResp;
import com.myc.train.common.utils.SnowUtil;
import com.myc.train.business.domain.Train;
import com.myc.train.business.domain.TrainExample;
import com.myc.train.business.mapper.TrainMapper;
import com.myc.train.business.req.TrainQueryReq;
import com.myc.train.business.req.TrainSaveReq;
import com.myc.train.business.resp.TrainQueryResp;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TrainService {

    @Resource
    private TrainMapper trainMapper;

    public void save(TrainSaveReq req){
        DateTime now = DateTime.now();
        Train train = BeanUtil.copyProperties(req, Train.class);

        if(ObjectUtil.isNull(train.getId())){
            //非空校验
            Train train1 = selectByUnique(req.getCode());
            if(ObjectUtil.isNotEmpty(train1)){
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_CODE_UNIQUE_ERROR);
            }

            train.setId(SnowUtil.getSnowflakeNextId());
            train.setCreateTime(now);
            train.setUpdateTime(now);
            trainMapper.insert(train);
        }else{
            train.setUpdateTime(now);
            trainMapper.updateByPrimaryKey(train);
        }
    }

    //唯一键校验
    public Train selectByUnique(String code){
        //定义查询条件
        TrainExample trainExample = new TrainExample();
        trainExample.createCriteria().andCodeEqualTo(code);
        List<Train> trains = trainMapper.selectByExample(trainExample);
        if(CollUtil.isNotEmpty(trains)){
            return trains.get(0);
        }else {
            return null;
        }
    }
    public PageResp<TrainQueryResp> queryList(TrainQueryReq req){
       //定义查询条件
        TrainExample trainExample = new TrainExample();
        //根据id倒序排序
        trainExample.setOrderByClause("id desc");
        //创建查询对象
        TrainExample.Criteria criteria = trainExample.createCriteria();
        //不为空则对其新增查询条件


        //新增分页信息
        PageHelper.startPage(req.getPage(),req.getSize());
        //根据条件查询结果
        List<Train> trains = trainMapper.selectByExample(trainExample);
        //new一个PageInfo并将结果放进来
        PageInfo<Train> trainPageInfo = new PageInfo<>(trains);
        //类拷贝
        List<TrainQueryResp> queryResps = BeanUtil.copyToList(trains, TrainQueryResp.class);
        //new一个PageResp
        PageResp<TrainQueryResp> resp= new PageResp<>();
        //放数据
        resp.setTotal(trainPageInfo.getTotal());
        resp.setList(queryResps);


        return resp;
    }

    public void delete(Long id){
        trainMapper.deleteByPrimaryKey(id);
    }

    @Transactional
    public List<TrainQueryResp> queryAll() {
//        //定义查询条件
//        TrainExample trainExample = new TrainExample();
//        trainExample.setOrderByClause("code asc");
//        //查询列表
//        List<Train> trains = trainMapper.selectByExample(trainExample);
        List<Train> trains = selectAll();
        List<TrainQueryResp> resps = BeanUtil.copyToList(trains, TrainQueryResp.class);
        return resps;

    }

    //查询所有火车
    public List<Train>  selectAll(){
        TrainExample trainExample = new TrainExample();
        trainExample.setOrderByClause("code asc");
        List<Train> trains = trainMapper.selectByExample(trainExample);
        return trains;
    }
}
